package com.zj.fx.service.month;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zj.fx.common.base.BaseService;
import com.zj.fx.common.support.ConvertUtil;
import com.zj.fx.mapper.month.MonthSupplytaskMapper;
import com.zj.fx.model.auto.TsysUser;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.model.info.InfoFile;
import com.zj.fx.model.month.MonthAuditSub;
import com.zj.fx.model.month.MonthConstant;
import com.zj.fx.model.month.MonthSupplytask;
import com.zj.fx.model.month.MonthSupplytaskExample;
import com.zj.fx.shiro.util.ShiroUtils;
import com.zj.fx.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 月评价供电所填报任务表 MonthSupplytaskService
 *
 * @author SQL_自动生成
 * @Title: MonthSupplytaskService.java 
 * @Package com.zj.fx.service 
 * @email ${email}
 * @date 2023-06-09 16:19:27  
 **/
@Service
public class MonthSupplytaskService implements BaseService<MonthSupplytask, MonthSupplytaskExample> {
    @Autowired
    private MonthSupplytaskMapper monthSupplytaskMapper;


    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public PageInfo<MonthSupplytask> list(Tablepar tablepar, MonthSupplytask monthSupplytask) {
        MonthSupplytaskExample testExample = new MonthSupplytaskExample();
        //搜索
        if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
            testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
        } else {//大搜索
            testExample.createCriteria().andLikeQuery(monthSupplytask);
        }
        //表格排序
        //if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
        //}else{
        //	testExample.setOrderByClause("id ASC");
        //}
        //根据当前登录账号查询自己供电所对应的填报数据
//        String supplyId = ShiroUtils.getDepId();
//        List<MonthSupplytask> newlist = list.stream().filter(s->s.getSupplyId().equals(supplyId)).collect(Collectors.toList());
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<MonthSupplytask> list = monthSupplytaskMapper.selectByExample(testExample);
        PageInfo<MonthSupplytask> pageInfo = new PageInfo<MonthSupplytask>(list);
        return pageInfo;
    }

    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public PageInfo<MonthSupplytask> list4self(Tablepar tablepar,MonthSupplytask monthSupplytask) {
        //根据当前登录账号查询自己供电所对应的填报数据
        String supplyId = ShiroUtils.getSupplyId();
        monthSupplytask.setSupplyId(supplyId);
        //分页查询
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<MonthSupplytask> list = monthSupplytaskMapper.query4self(monthSupplytask);
        PageInfo<MonthSupplytask> pageInfo = new PageInfo<MonthSupplytask>(list);
        return pageInfo;


    }

    @Override
    public int deleteByPrimaryKey(String ids) {

        List<String> lista = ConvertUtil.toListStrArray(ids);
        MonthSupplytaskExample example = new MonthSupplytaskExample();
        example.createCriteria().andIdIn(lista);
        return monthSupplytaskMapper.deleteByExample(example);


    }


    @Override
    public MonthSupplytask selectByPrimaryKey(String id) {

        return monthSupplytaskMapper.selectByPrimaryKey(id);

    }


    @Override
    public int updateByPrimaryKeySelective(MonthSupplytask record) {
        return monthSupplytaskMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(MonthSupplytask record) {

        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());


        return monthSupplytaskMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(MonthSupplytask record, MonthSupplytaskExample example) {

        return monthSupplytaskMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(MonthSupplytask record, MonthSupplytaskExample example) {

        return monthSupplytaskMapper.updateByExample(record, example);
    }

    @Override
    public List<MonthSupplytask> selectByExample(MonthSupplytaskExample example) {

        return monthSupplytaskMapper.selectByExample(example);
    }


    @Override
    public long countByExample(MonthSupplytaskExample example) {

        return monthSupplytaskMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(MonthSupplytaskExample example) {

        return monthSupplytaskMapper.deleteByExample(example);
    }

    public List<MonthSupplytask> getDistinctSubbyMonth(MonthSupplytask monthSupplytask) {
        return monthSupplytaskMapper.getDistinctSubbyMonth(monthSupplytask);
    }

    public PageInfo<MonthSupplytask> list4FillPlan(String evaluationId, Tablepar tablepar, MonthSupplytask monthSupplytask) {
        MonthSupplytaskExample testExample = new MonthSupplytaskExample();
        //搜索
        if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
            testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
        } else {//大搜索
            testExample.createCriteria().andLikeQuery(monthSupplytask);
        }
        testExample.createCriteria().andFkEvaluationIdEqualTo(evaluationId);
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<MonthSupplytask> list = monthSupplytaskMapper.selectByExample(testExample);
        PageInfo<MonthSupplytask> pageInfo = new PageInfo<MonthSupplytask>(list);
        return pageInfo;
    }

    /**
     * 查询分公司下的供电所（填报状态）
     * @param evaluationId
     * @param fkSubcompanyId
     * @param state
     * @param tablepar
     * @return
     */
    public PageInfo<MonthSupplytask> supplyList(String evaluationId, String fkSubcompanyId, String state, Tablepar tablepar) {
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        MonthSupplytaskExample monthSupplytaskExample = new MonthSupplytaskExample();
        MonthSupplytaskExample.Criteria criteria = monthSupplytaskExample.createCriteria();
        criteria.andFkEvaluationIdEqualTo(evaluationId).andFkSubcompanyIdEqualTo(fkSubcompanyId);
        if (MonthConstant.TASK_STATE_NOFILL.equals(state)){
            criteria.andTaskStateEqualTo(state);
        }else {
            criteria.andTaskStateNotEqualTo(MonthConstant.NOT);
        }
        List<MonthSupplytask> list = monthSupplytaskMapper.selectByExample(monthSupplytaskExample);
        return new PageInfo<MonthSupplytask>(list);
    }

    public List<InfoFile> filelist4HistoryByDtlNumandTaskid(MonthSupplytask monthSupplytask) {
        return monthSupplytaskMapper.filelist4HistoryByDtlNumandTaskid(monthSupplytask);
    }

}
